﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;
using System.IO;
using System.ComponentModel;

namespace DataGui
{
    /// <summary>
    /// Interaction logic for ShowResults.xaml
    /// </summary>
    public partial class ShowResults : Window
    {
        private MainWindow main;

        public ShowResults(MainWindow main, String outp)
        {
            InitializeComponent();
            this.main = main;
            showSR(outp);
        }

        private void showSR(string outp)
        {
            if(MainWindow.SRshow){
                SRgridLable.Visibility = System.Windows.Visibility.Hidden;
                int i = outp.IndexOf("SR_SSD ");
                int j = outp.IndexOf("\n",i);
                LssdNum.Content = outp.Substring(i+7,j-i-7);
                i = outp.IndexOf("SR_FC ");
                j = outp.IndexOf("\n", i);
                LfcNum.Content = outp.Substring(i + 6, j - i - 6);
                i = outp.IndexOf("SR_SATA ");
                j = outp.IndexOf("\n", i);
                LsataNum.Content = outp.Substring(i + 8, j - i - 8);
            }
            else
            {
               SRgrid.Visibility = System.Windows.Visibility.Hidden;
            }
                
            
        }

        private void Bback_Click(object sender, RoutedEventArgs e)
        {
            foreach (System.Diagnostics.Process myProc in System.Diagnostics.Process.GetProcesses())
                if (myProc.ProcessName == "ExcelReport")
                    myProc.Kill();
            App.Current.MainWindow = main;
            this.Close();
            main.Show();
        }

        private void BgetGraphs_Click(object sender, RoutedEventArgs e)
        {
            if ((bool)CBnext.IsChecked || (bool)CBdivision.IsChecked ||
                (bool)CButil.IsChecked || (bool)CBlcorrelation.IsChecked ||
                (bool)CBrt.IsChecked || (bool)CBpriority.IsChecked)
            {
                writeExcelsInputFile();
                runExcelReport();
            }
        }


        public void runExcelReport()
        {
            // Use ProcessStartInfo class
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.WorkingDirectory = "..\\..\\..\\ExcelReport\\bin\\Release";
            startInfo.Domain = "..\\..\\..\\ExcelReport\bin\\Release";
            startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute = false;
            startInfo.FileName = "..\\..\\..\\ExcelReport\\bin\\Release\\ExcelReport.exe";
            startInfo.WindowStyle = ProcessWindowStyle.Minimized;
            startInfo.CreateNoWindow = true;
            try
            {
                using (Process exeProcess = Process.Start(startInfo))
                {
                    
                    int exitCode = exeProcess.ExitCode;
                }
            }
            catch
            {
                int c = 0;
            }
        }

        private void writeExcelsInputFile()
        {
            TextWriter cfgFile = new StreamWriter("..\\..\\..\\DataBase\\graphs.txt");

            cfgFile.Write("damage\t");
            if((bool)main.RBoptimal.IsChecked)
                cfgFile.Write("Optimal\t");
            if ((bool)main.RBnext.IsChecked)
                cfgFile.Write("Next\t");
            if ((bool)main.RBcost.IsChecked)
                cfgFile.Write("Cost\t");
            if ((bool)CBnext.IsChecked)
                cfgFile.WriteLine("1");
            else cfgFile.WriteLine("0");

            cfgFile.Write("utilization\t");
            if ((bool)CButil.IsChecked)
                cfgFile.WriteLine("1");
            else cfgFile.WriteLine("0");

            cfgFile.Write("rt\t");
            if ((bool)CBrt.IsChecked)
                cfgFile.WriteLine("1");
            else cfgFile.WriteLine("0");

            cfgFile.Write("priority\t");
            if ((bool)CBpriority.IsChecked)
                cfgFile.WriteLine("1");
            else cfgFile.WriteLine("0");

            cfgFile.Write("division\t");
            if ((bool)CBdivision.IsChecked)
                cfgFile.WriteLine("1");
            else cfgFile.WriteLine("0");

            cfgFile.Write("lcorrelation\t");
            if ((bool)CBlcorrelation.IsChecked)
                cfgFile.Write("1");
            else cfgFile.Write("0");

            cfgFile.Close();

        }

    }
}
